/*#define _POSIX_SOURCE*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

#include "helper.h"

/* loads the environment variables in PROFILE*/
int loadprof(){
            /*each line in PROFILE is simply a setenv assignment
    So all we have to do is read line by line and use
    setenv call */
    char* name;
    char* val;
    FILE *file;
    
    static const char filename[] = "PROFILE";
    
    file = fopen ( filename, "r" );
    
    if ( file != NULL ) {
        char line [ 128 ]; /* or other suitable maximum line size */
    
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {           
            name = strtok(line," =");
            val = strtok(NULL," =");
            setenv(name,val,1);
        
        }
        fclose ( file );
    }
    else    {
        perror ( filename ); /* why didn't the file open? */
        printf ( "PROFILE not found, default environment used" ); 
        setenv("PROMPT_SIGN","myshell",1);
    }
    
    
    return 0;
}

void login(){
    char* username;
    char* password;
    char user[40];
    char pass[40];    
    char* trgt;
    char fpath[40];
    int f1=0,f2=0;
    username = getenv("USERNAME");    
    password = getenv("PASSWORD");
    password = strtok(password,"\n");
    trgt=getenv("HOME");
    trgt = strtok(trgt,"\n");
    
    
    
    strcpy(fpath,"chdir ");
    
    strncat(fpath,trgt,5);
    
    
    fflush(stdout);
    
    
    printf("Enter username...");
    
    
        fgets(user,40,stdin);
        

                if(strcmp(user,username)!=0)
                {
                    printf("The username is wrong");
                    exit(0);
                }

                else
                {
                    f1=1;
                }
        
        
    

    printf("Enter password...");


        gets(pass);
       printf("entered: |%s|%s|",pass,password);
                if(strcmp(pass,password)!=0)
                {
                    printf("The password is wrong\n");
                    exit(0);
                }

                else
                {
                    printf("\nLogin successful!!Welcome to MiniX shell!\n");
                    f2=1;
                }
    
    system(fpath);

}


/***********************
 * Other helper routines
 ***********************/

/*
 * usage - print a help message
 */
void usage(void) 
{
    printf("Usage: shell [-hvp]\n");
    printf("   -h   print this message\n");
    printf("   -v   print additional diagnostic information\n");
    printf("   -p   do not emit a command prompt\n");
    exit(1);
}

/*
 * unix_error - unix-style error routine
 */
void unix_error(char *msg)
{
    fprintf(stdout, "%s: %s\n", msg, strerror(errno));
    exit(1);
}

/*
 * app_error - application-style error routine
 */
void app_error(char *msg)
{
    fprintf(stdout, "%s\n", msg);
    exit(1);
}

/*
 * Signal - wrapper for the sigaction function
 */
handler_t *Signal(int signum, handler_t *handler) 
{
    struct sigaction action, old_action;

    action.sa_handler = handler;  
    sigemptyset(&action.sa_mask); /* block sigs of type being handled */
    action.sa_flags = SA_RESTART; /* restart syscalls if possible */

    if (sigaction(signum, &action, &old_action) < 0)
        unix_error("Signal error");
    return (old_action.sa_handler);
}

/*
 * sigquit_handler - The driver program can gracefully terminate the
 *    child shell by sending it a SIGQUIT signal.
 */
void sigquit_handler(int sig) 
{
    printf("Terminating after receipt of SIGQUIT signal\n");
    exit(1);
}
